<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

class Authority extends Admin
{
    protected $popup_size = ['width' => '500px', 'height' => '600px'];

    public function initialize()
    {

        $this->menu_input = [
            'all' => [
                ['type' => 'select', 'name' => '所属权限', 'key' => 'parent_id', 'verify' => 'required',
                    'select' => [
                        'table' => 'Authority'
                    ],
                    'dg' => 1
                ],
                ['type' => 'text', 'name' => '标题', 'key' => 'title', 'verify' => 'required'],
                ['type' => 'text', 'name' => '模块', 'key' => 'module'],
                ['type' => 'text', 'name' => '控制器', 'key' => 'controller'],
                ['type' => 'text', 'name' => '方法', 'key' => 'method'],
                ['type' => 'text', 'name' => 'URL参数', 'key' => 'parameter'],
                ['type' => 'icon', 'name' => '图标', 'key' => 'icon'],

            ],
            'add' => [
            ],
            'upload' => [
            ],
        ];
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function table_mage(&$data = [])
    {

        $data = [
            ['field' => 'title', 'title' => '标题'],
            ['field' => 'module', 'title' => '模块'],
            ['field' => 'controller', 'title' => '控制器'],
            ['field' => 'method', 'title' => '方法'],
            ['field' => 'parameter', 'title' => '参数'],
            ['field' => 'level', 'title' => '层级'],
            ['field' => 'icon', 'title' => '图标', 'templet' => '#list_icon', 'width' => '4%'],
            ['field' => 'create_time', 'title' => '添加时间'],
            ['field' => 'update_time', 'title' => '修改时间'],
            ['title' => '操作', 'templet' => '#list_setting', 'width' => '9%']
        ];
        return parent::table_mage($data); // TODO: Change the autogenerated stub
    }

    //添加-数据接口
    public function add_ajax()
    {

        if (request()->isAjax()) {
            $post = array_filter(input('post.'));
            if ($post) {
                $pid = $this->model->where('id', $post['parent_id'])->find()->toArray();
                $post['level'] = $pid['level'] * 1 + 1;
                if (!empty($user)) {
                    return msgerror('该账户已存在添加失败');
                }
                $this->model->save($post);
            } else {
                return msgerror('参数缺失');
            }
            return datasuccess($post);
        }
    }
}
